package com.bitjsc.server;

import java.util.Arrays;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;

import com.bitjsc.server.data.Player;
import com.bitjsc.server.data.Room;



public class RedServerTest extends ApplicationAdapter
{
    private static final Log log = LogFactory.getLog(RedServerTest.class);
    public HashMap<String, Room> playRoom = new HashMap<String, Room>();
    public HashMap<String, Player> players = new HashMap<String, Player>();
    public Player player = null;
    
    public String[] appStart(IConnection conn)
    {
    	log.info("RedTest.appStart");
    	this.playRoom.put("1",new Room("VIP I"));
    	this.playRoom.put("2", new Room("VIP II"));
    	this.playRoom.put("3", new Room("VIP III"));
    	this.playRoom.put("r1", new Room("Room I"));
    	this.playRoom.put("r2", new Room("Room II"));
    	this.playRoom.put("r3", new Room("Room III"));
    	this.playRoom.put("r4", new Room("Room IV"));
    	this.playRoom.put("r5", new Room("Room V"));
    	    	
    	int size = this.playRoom.size();
    	String[] arrRoom = new String[size];
    	Object[] key = playRoom.keySet().toArray();
    	Arrays.sort(key);
    	int i =0;
    	
    	/*while (itr.hasNext())
		{
    		
    		arrRoom[i++] = playRoom.get(itr.next()).getName();
		}*/
    	while(i<key.length)
    	{
    		arrRoom[i] = playRoom.get(key[i++]).getName();
    		
    	}
    	return arrRoom;
    }
    
    public void appStop()
    {
    	log.info("RedTest.appStop");
    	
    }
    public boolean appConnect(IConnection conn,Object[] params)
    {
    	System.out.println("RedTest.appConnect :"+ conn.getClient().getId());
    	return true;
    }
    public void appDisconnect(IConnection conn)
    {
    	log.info("RedTest.appDisconnect : "+conn.getClient().getId());
    	super.appDisconnect(conn);
    }
    
    public Player getCurrentPlayer(IConnection conn)
    {
    	IClient client = conn.getClient();
    	player = new Player();
    	player  = players.get(client.getId());
    	return player;
    }
    
    public Boolean verifyUsrPwd(IConnection conn,String usr,String pwd)
    {
    	
    	
    	if(usr.equals("thoiht") && pwd.equals("1"))
    	{
	    	System.out.println("Client : "+ usr +" Pass : "+pwd);
	    	Player player = new Player(conn,usr);
	    	this.players.put(player.getClientID(), player);
	    	player.invoke("createClientPlayer", new Object[]{player.getClientID(),player.getName()});
	    	System.out.println("Client: "+player.getName()+" connected");
	    	return true;
    	}
    	else
    	{
    		System.out.println("Invalid Username or Password...");
        	return false;
    	}
    	
    	
    }
    
    public Boolean createPlayerList(IConnection conn,String roomName,String roomType)
    {
    	System.out.println(roomName);
    	player = this.getCurrentPlayer(conn);
    	player.setCurrentRoom(new Room(roomName));
    	player.getCurrentRoom().invokeAll("updatePlayerInfo",new Object[]{player.getClientID(),player.getName(),player.getSex(),player.getLocation(),player.getMoney(),player.getViPlayer()});
    	return player.getViPlayer();
    }
    
    
}